<?php
/**
 * Created by PhpStorm.
 * User: Qarluq
 * Date: 15/7/3
 * Time: 下午5:45
 */
   class IndexAction extends Action{
       public function index(){
           $db=D('ContentView');
           import('Class.Category',APP_PATH);
           $cate=M('category')->order('sort ASC')->select();
           $this->cate=Category::unlimitedforlayer($cate);
           $where[0]=array('name'=>'wen_jian_tong_zhi','accept'=>1);
           $where[1]=array('name'=>'zhou_gong_zuo_an_pai','accept'=>1);
           $where[2]=array('con_category'=>'wang_zhan_gong_gao','accept'=>1);
           $where[3]=array("pid"=>13,'accept'=>1);
           $where[4]=array("pid"=>11,'con_category'=>array('neq','zhou_gong_zuo_an_pai'),'accept'=>1);
           $where[5]=array("con_category"=>'shi_sheng_zuo_pin','accept'=>1);
           $where[6]=array("pid"=>12,'accept'=>1);
           $where[7]=array("con_category"=>'jiao_yan_huo_dong','accept'=>1);
           $where[8]=array("con_category"=>'ke_wai_huo_dong','accept'=>1);
           $where[9]=array("con_category"=>'xiao_yuan_shhi_pin','accept'=>1);
           $where[10]=array("con_category"=>'jiao_an_gong_xiang','accept'=>1);
           $where[11]=array("con_category"=>'ke_jian_gong_xiang','accept'=>1);
           $where[12]=array("con_category"=>'shi_zhuan_gong_xiang','accept'=>1);
           $where[13]=array("con_category"=>'huan_deng_pian','accept'=>1);
           $xiao_wu_gong_kai['pid'] = 10;
           $xiao_wu_gong_kai['accept'] = 1;
           $xiao_wu_gong_kai['con_category'] = array(array('neq','wen_jian_tong_zhi'),array('neq','zhou_gong_zuo_an_pai'));
           $this->wen_jian=$db->where($where[0])->order('add_time desc')->limit(10)->select();
           $this->zui_xin=$db->where($where[13])->order('id DESC')->limit(8)->select();
           $this->zhou_gong_zuo_an_pai=$db->where($where[1])->order('id desc')->limit(10)->select();
           $this->gong_gao=M('content')->where($where[2])->order('id desc')->limit(4)->select();
           $this->ri_men=M('content')->order('count desc')->limit(13)->select();
           $this->xiao_wu_gong_kai=$db->where($xiao_wu_gong_kai)->order('id desc')->limit(10)->select();
           $this->jiao_yu_jiao_xue_1=$db->where($where[4])->order('id desc')->limit(0,11)->select();
           $this->jiao_yu_jiao_xue_2=$db->where($where[4])->order('id desc')->limit(11,11)->select();
           $this->shi_sheng_zuo_pin=$db->where($where[5])->order('id DESC')->limit(10)->select();
           $this->dang_jian_gong_zuo=$db->where($where[6])->order('id DESC')->limit(10)->select();
           $this->jiao_yu_huo_dong=$db->where($where[7])->order('id DESC')->limit(10)->select();
           $this->ke_wai_huo_dong=$db->where($where[8])->order('id DESC')->limit(4)->select();
           $this->shi_sheng_feng_cai=$db->where("con_category='jiao_shi_feng_cai' OR con_category='xue_sheng_feng_cai'")->order('id DESC')->limit(20)->select();
           $this->xiao_yuan_shi_pin=$db->where($where[9])->order('id DESC')->limit(8)->select();
           $this->de_yu_chuang=$db->where($where[3])->order('id desc')->limit(4)->select();
           $this->you_qing_lian_jie_01=M('link')->order('id desc')->limit(0,9)->select();
           $this->you_qing_lian_jie_02=M('link')->order('id desc')->limit(9,9)->select();
           $this->jiao_an_gong_xiang=$db->where($where[10])->order('id desc')->limit(10)->select();
           $this->ke_jian_gong_xiang=$db->where($where[11])->order('id desc')->limit(10)->select();
           $this->shi_juan_gong_xiang=$db->where($where[12])->order('id desc')->limit(10)->select();
           $this->display();
       }
       /*
        * */
       public function sendmail(){
           import('Class.Category',APP_PATH);
           $cate=M('category')->order('sort ASC')->select();
           $this->cate=Category::unlimitedforlayer($cate);
           $this->you_qing_lian_jie_01=M('link')->order('id desc')->limit(0,12)->select();
           $this->you_qing_lian_jie_02=M('link')->order('id desc')->limit(12,12)->select();
           $this->ri_men=M('content')->order('count desc')->limit(13)->select();
           if(I('level')==1){
               $to=C('school_master_email');
               $name='校长信箱';
           }
           if(I('level')==2){
               $to=C('school_secretary_email');
               $name='书记信箱';
           }
           $this->to=$to;
           $this->name=$name;
           $this->display('email_sender');
       }
       public function addmember(){
           import('Class.Category',APP_PATH);
           $cate=M('category')->order('sort ASC')->select();
           $this->cate=Category::unlimitedforlayer($cate);
           $this->ri_men=M('content')->order('count desc')->limit(13)->select();
           $this->you_qing_lian_jie_01=M('link')->order('id desc')->limit(0,9)->select();
           $this->you_qing_lian_jie_02=M('link')->order('id desc')->limit(9,9)->select();
           $this->display('register');
       }
       public function run_addmember(){


           $user = M('member')->where(array('mem_name' => array('eq', I('mem_name'))))->select();
           if ($user) {
               $this->error('账户已存在');
           }

           if (I('mem_password_1') != I('mem_password')) {
               $this->error('两次密码不配');
           }

           $data=array(
               "mem_name" => I('mem_name'),
               "real_name" => I('real_name'),
               "mem_phone" => I('mem_phone'),
               "mem_email" => I('mem_email'),
               "mem_password" => I('mem_password','','md5'),
               "mem_number" => I('mem_number'),
               "lock" => I('lock'),
               "reg_time"=>date('Y-m-d-h:i:s',time()),
               "reg_ip"=>get_client_ip()
           );
           $db=M('member')->add($data);
           if($db) {
               $this->success('发送请求成功！',U('index'));
           }else{
               $this->error('发送请求失败');
           }
       }
       public function run_sendmail(){
           $content=implode(',',$_POST['content']);

           if(SendMail($_POST['mail'],$_POST['title'],$content))
               $this->success('发送成功！');
           else
               $this->error('发送失败');
       }
       public function verify(){
           import('Class.Image',APP_PATH);
           Image::verify();
       }
       public function logout(){
           session_unset();
           session_destroy();

           if(!isset($_SESSION['uid'])){
               echo "<script>alert('安全退出');</script>";
               $this->redirect(GROUP_NAME.'/Index/index');
           }
       }
       public function run_login()
       {
           /*adris arqiliq kiriwallghan bolsa*/
           if (!IS_POST) halt('页面不存在');
           /*testiq kodi toghura bolmisa*/
           if (I('code', '', 'md5') != session('verify')) $this->error('验证码错误');

           /*abuntning ismini sandandin izdeymiz*/
           $condition['mem_name'] = I('mem_name');
           /*munasiwetlik uchurlarni suzup alimiz*/
           $user = M('member')->where($condition)->find();

           /*quluplanghan yaki emeslikini tekshureymiz*/
           if ($user['mem_status']==0) $this->error('账户还没验证');
           /*quluplanghan yaki emeslikini tekshureymiz*/
           if ($user['lock']==0) $this->error('账户已被锁定');
           /*waqitliq quluplanghan liqini tekshureymiz*/
           if ($user['logintime'] > time()) {
               $this->error('账户名已被暂时锁定一个小时后请重试');
           }
           /*eger xata kirguzse munasiwetlik uchur qayturmiz  bash qetimdin eship ketse waqitliq quluplaymiz*/
           if (!$user || $user['mem_password'] != I('mem_password', '', 'md5')) {
               $_SESSION['lock_number'] = $_SESSION['lock_number'] + 1;
               if ($_SESSION['lock_number'] >= 4) {
                   $data_time = array(
                       'id' => $user['id'],
                       'logintime' => time() + 3600,
                   );
                   M('member')->save($data_time);
               }
              $this->error('账户名或密码错误');
           }
           $data = array(
               'id' => $user['id'],
               'loginip' => get_client_ip(),
               'logintime' => time(),
           );
           /*kirgen waqtini ip adressininxatirleymiz  andin betke kirguzwetimiz*/
           M('member')->save($data);
           session('teacherid', $user['id']);
           session('teachername', $user['mem_name']);
           session('logintime', $user['logintime']);
           session('loginip', $user['loginip']);
           $this->success("登陆成功欢迎你 ".session('teachername'));
       }
       public function editmemberinfo(){
           import('Class.Category',APP_PATH);
           $cate=M('category')->order('sort ASC')->select();
           $this->cate=Category::unlimitedforlayer($cate);
           $this->you_qing_lian_jie_01=M('link')->order('id desc')->limit(0,12)->select();
           $this->you_qing_lian_jie_02=M('link')->order('id desc')->limit(12,12)->select();
           $this->ri_men=M('content')->order('count desc')->limit(13)->select();
           if(!isset($_SESSION['teachername'])){
               $this->error('你还没有登陆');
           }
           $data=M('member')->where(array('mem_name' => array('eq', session('teachername'))))->select();
           $this->data=$data;
            $this->display('change_info');
           // $this->display('');

       }
       public function editmemberpwd(){
           import('Class.Category',APP_PATH);
           $cate=M('category')->order('sort ASC')->select();
           $this->cate=Category::unlimitedforlayer($cate);
           $this->you_qing_lian_jie_01=M('link')->order('id desc')->limit(0,12)->select();
           $this->you_qing_lian_jie_02=M('link')->order('id desc')->limit(12,12)->select();
           $this->ri_men=M('content')->order('count desc')->limit(13)->select();
           if(!isset($_SESSION['teachername'])){
               $this->error('你还没有登陆');
           }
           $data=M('member')->where(array('mem_name' => array('eq', session('teachername'))))->select();
           $this->data=$data;
           $this->display('change_password');

       }
       public function run_editmember()
       {
           $old_user = M('member')->where(array('mem_name' => array('eq', session('teachername'))))->find();
           if(isset($_POST['mem_password'])){
               /*
             * maxpi numurini sandangha selishturdum*/
               if ($old_user['mem_password'] != I('old_password', '', 'md5')) {
                   $this->error('您的久密码不准');
               }

               /*ikki parolini selishturdum*/
               if (I('password') != I('mem_password')) {
                   $this->error('密码确认失败');
               }

               $data=array(
                   'id' => $old_user['id'],
                   "mem_password" => I('mem_password','','md5'),
               );

           }else{
               $data=array(
                   'id' => $old_user['id'],
                   "mem_name" => session('teachername'),
                   "mem_phone" => I('mem_phone'),
                   "mem_email" => I('mem_email'),
                   "mem_number" => I('mem_number'),
               );

           }

           /*
            * uchurni sanghangha qoshtum*/
           if (M('member')->save($data)) {
               $this->success('修改成功', U(GROUP_NAME . '/Index'));
           } else {
               $this->error('修改失败');
           }


       }
   }